%%%-------------------------------------------------------------------
%%% @author flowerpig
%%% @copyright (C) 2020, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 09. 5月 2020 5:22 下午
%%%-------------------------------------------------------------------
-module(web_assist).
-author("flowerpig").
-include("../include/player.hrl").
-include("../include/game.hrl").
-include("../include/protocol.hrl").
-include("../include/logger.hrl").
%% API
-export([get_player_state/1, get_room_state/2]).

%%根据账号获取用户状态
-spec(get_player_state(Account :: string()) -> #player_state{}).
get_player_state(Account) ->
  Player_pid = list_to_atom("pid_" ++ Account),
  gen_server:call(Player_pid, {player_state}).


%%获取用户游戏房间状态
-spec(get_room_state(Account :: string(), Game_id :: integer()) -> #{}).
get_room_state(Account, Game_id) ->
  Game_pid = list_to_atom("pid_" ++ Account ++ "_" ++ integer_to_list(Game_id)),
  Room_state = gen_server:call(Game_pid, get_state),
  case erlang:is_record(Room_state, room_status) of
    true ->
      Map = #{player_id => Room_state#room_status.player_id,
        account => Account, game_id => Game_id,
        index => Room_state#room_status.index,
        player_state => Room_state#room_status.player_state,
        is_queue => Room_state#room_status.is_queue,
        is_integral => Room_state#room_status.is_integral,
        total_plate => Room_state#room_status.total_plate,
        room_score => Room_state#room_status.room_score,
        left_score => Room_state#room_status.left_score},
      Map2 =
        maps:map(fun(_Key, Val) ->
          case is_list(Val) of
            true -> list_to_binary(Val);
            _ -> Val
          end
                 end, Map),
      Map2;
    _ -> #{}
  end.






